package br.edu.ufcg.boombox.view;

import br.edu.ufcg.boombox.controller.CadastroUsuarioController;
import br.edu.ufcg.boombox.util.Validadores;
import javax.swing.JOptionPane;
import br.edu.ufcg.boombox.model.CadastroUsuarioModel;
import br.edu.ufcg.boombox.model.RepositoryManager;
import br.edu.ufcg.boombox.model.Usuario;


/**
 *
 * @author Silva
 */
public class CadastroUsuarioView extends javax.swing.JFrame {
   
    /**
     * Creates new form CadastroUsuarioView
     */
    public CadastroUsuarioView() {
        initComponents();
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelNome = new javax.swing.JLabel();
        fieldNome = new javax.swing.JTextField();
        labelEmail = new javax.swing.JLabel();
        fieldEmail = new javax.swing.JTextField();
        labelSenha = new javax.swing.JLabel();
        passFieldSenha = new javax.swing.JPasswordField();
        comboBoxGenero = new javax.swing.JComboBox();
        labelGenero = new javax.swing.JLabel();
        buttonSalvar = new javax.swing.JButton();
        buttonCancelar = new javax.swing.JButton();
        labelCabecalho = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Usuario");
        setBackground(new java.awt.Color(153, 153, 153));
        setMinimumSize(new java.awt.Dimension(600, 400));

        labelNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelNome.setText("Nome:");

        fieldNome.setToolTipText("Digite seu nome completo");

        labelEmail.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelEmail.setText("E-mail:");

        labelSenha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelSenha.setText("Senha:");

        passFieldSenha.setToolTipText("Digite sua senha com no máximo 10 caracteres.");

        comboBoxGenero.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Feminino" }));

        labelGenero.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelGenero.setText("Gênero:");

        buttonSalvar.setText("Salvar");
        buttonSalvar.setActionCommand("cadastrar");

        buttonCancelar.setText("Cancelar");
        buttonCancelar.setActionCommand("cancelar");

        labelCabecalho.setText("      Criar uma nova conta BoomBox, insira seus dados.");
        labelCabecalho.setMaximumSize(new java.awt.Dimension(200, 14));
        labelCabecalho.setMinimumSize(new java.awt.Dimension(200, 14));
        labelCabecalho.setPreferredSize(new java.awt.Dimension(200, 14));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(167, 167, 167)
                        .addComponent(buttonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelGenero)
                                .addGap(18, 18, 18)
                                .addComponent(comboBoxGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(labelEmail)
                                    .addComponent(labelNome))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(fieldEmail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(fieldNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelSenha)
                                .addGap(24, 24, 24)
                                .addComponent(passFieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(0, 43, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(125, 125, 125)
                .addComponent(labelCabecalho, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(labelCabecalho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(fieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelEmail))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passFieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSenha))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelGenero)
                    .addComponent(comboBoxGenero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSalvar)
                    .addComponent(buttonCancelar))
                .addGap(65, 65, 65))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuarioView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuarioView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuarioView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuarioView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new CadastroUsuarioView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancelar;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JComboBox comboBoxGenero;
    private javax.swing.JTextField fieldEmail;
    private javax.swing.JTextField fieldNome;
    private javax.swing.JLabel labelCabecalho;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelGenero;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelSenha;
    private javax.swing.JPasswordField passFieldSenha;
    // End of variables declaration//GEN-END:variables


    public void initFieldNome() {
        this.fieldNome.setActionCommand("fieldNome");
    }
    
    public void initFieldEmail() {
        this.fieldEmail.setActionCommand("fieldEmail");
    }
    
    public void initPassFieldSenha() {
        this.passFieldSenha.setActionCommand("fieldSenha");        
    }
    
    public void initComboBoxGenero() {
        this.comboBoxGenero.setActionCommand("comboBoxGenero");
    }
    
    public void initButtonCancelar() {
        this.buttonCancelar.setActionCommand("cancelar");
    }
    
    public void initButtonSalvar() {
        this.buttonSalvar.setActionCommand("cadastrar");
    }
    
    private void initActionComands() {
        initFieldNome();
        initFieldEmail();
        initPassFieldSenha();
        initComboBoxGenero();
        initButtonCancelar();
        initButtonSalvar();
    }

    public CadastroUsuarioModel lerModelo() {
        
        String nome = this.fieldNome.getText();
        String email = this.fieldEmail.getText();
        String senha = new String(this.passFieldSenha.getPassword());
        String genero = (String) this.comboBoxGenero.getSelectedItem();
        
        if(!Validadores.validarEmail(email)) {
            JOptionPane.showMessageDialog(null, "Insira um email válido!");
        } else if(RepositoryManager.getInstance().existeEmail(email)){
            JOptionPane.showMessageDialog(null, "Já existe um usuário cadastrado com esse email!");
        } else if( !(senha.length() >= 6 && senha.length() <= 12) ) {
            JOptionPane.showMessageDialog(null, "A senha deve possuir dentre seis a doze caracteres!");

        } else if ( Validadores.validarSenhaForte(senha) ){
            CadastroUsuarioModel modelo = new CadastroUsuarioModel();
            modelo.setNome(nome);   
            modelo.setEmail(email);
            modelo.setSenha(senha);
            modelo.setGenero(genero);
            return modelo;
        }
        
        return null;
    }

    public void clear() {
        this.fieldNome.setText("");
        this.fieldEmail.setText("");
        this.passFieldSenha.setText("");
        this.comboBoxGenero.setSelectedIndex(0);
    }

    public void configuraOuvinte(CadastroUsuarioController controlador) {
        this.fieldNome.addActionListener(controlador);
        this.fieldEmail.addActionListener(controlador);
        this.passFieldSenha.addActionListener(controlador);
        this.comboBoxGenero.addActionListener(controlador);
        this.buttonSalvar.addActionListener(controlador);
        this.buttonCancelar.addActionListener(controlador);
    }
    
}
